package com.sun;

import javax.imageio.IIOException;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 烛龙神
 * @Description TODO
 * @date 2021-04-07 21:48
 */
public class SocketService {

    public static void main(String[] args) throws IOException {
        try
        {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已启动......");
            Socket socket = serverSocket.accept();
            System.out.println("客户端:"+socket.getInetAddress().getHostAddress()+"已经连接到服务器");

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String str = bufferedReader.readLine();
            System.out.println("客户端:"+str);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedWriter.write("客户端你好，我是服务端" + "\n");
            bufferedWriter.flush();
        }catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}
